# -*- coding: utf-8 -*-
"""
========================================================================================================================
@project : AosSanic
@file: OAClient
@Author: mengying
@email: 652044581@qq.com
@date: 2022/4/3 10:20
@desc: O2OA组织相关接口
========================================================================================================================
"""

from OAClient import O2OAClient
from config import config
from OAError import O2OAError
import requests, json


class O2OAUnit(O2OAClient):
    """020A组织相关"""

    def __init__(self):
        super(O2OAUnit, self).__init__()

    def create(self, name, unique, superior=None, type="company"):
        """
        @desc: 新建一个组织
        @params: name 组织的名字
        @params： unique 组织的唯一编码
        @params: type 组织类型可以写死
        @params: superior父节点,如果是创建子节点需要传
        @type： 组织类别
        @return: 返回组织unit_id
        """
        url = config.O2OA_URL + "/x_organization_assemble_control/jaxrs/unit"
        data = {
            "name": name,
            "unique": unique,
            "typeList": [type],
            "description": "",
            "shortName": "",
            "superior": "",
            "orderNumber": "",
            "controllerList": [],
            "control": {"allowEdit": True,
                        "allowDelete": True},
            "woSubDirectIdentityList": [],
            "woUnitAttributeList": [],
            "woUnitDutyList": []
        }

        if superior:
            data.update({"superior": superior})

        response = requests.post(url=url, headers=self.o2oa_headers, data=json.dumps(data)).json()
        types = response.get("type", "")
        if types != "success":
            raise O2OAError(response.get("message", ""))

        return response['data']['id']

    def delete(self, unit_id):
        """
        @desc: 删除该组织
        @params: unit_id 组织的id
        """
        url = config.O2OA_URL + "/x_organization_assemble_control/jaxrs/unit/" + unit_id
        response = requests.delete(url, headers=self.o2oa_headers).json()
        types = response.get("type", "")
        if types != "success":
            raise O2OAError(response.get("message", ""))

        return response['data']

    def list(self, unit_id):
        """
        @desc: 获取组织信息
        @params: unit_id 组织的id
        @return: 返回组织信息，包含人员信息，和上级组织信息
        """
        url = config.O2OA_URL + "/x_organization_assemble_control/jaxrs/unit/" + unit_id
        response = requests.get(url=url, headers=self.o2oa_headers).json()
        types = response.get("type", "")
        if types != "success":
            raise O2OAError(response.get("message", ""))

        return response['data']


class O2OAUnitStaff(O2OAClient):

    def __init__(self):
        super(O2OAUnitStaff, self).__init__()

    def create(self, staff_name, staff_id, unit_id):
        """
        @desc: 给组织中添加人员
        @params: staff_name 人员名字
        @params： staff_id 人员id
        @params: unit_id 组织id
        """
        url = config.O2OA_URL + "/x_organization_assemble_control/jaxrs/identity"
        data = {
            "description": "",
            "name": staff_name,
            "unique": "",
            "person": staff_id,
            "department": "",
            "unit": unit_id,
            "orderNumber": ""
        }

        response = requests.post(url=url, headers=self.o2oa_headers, data=json.dumps(data)).json()
        types = response.get("type", "")
        if types != "success":
            raise O2OAError(response.get("message", ""))
        return response['data']['id']

    def delete(self, user_unit_id):
        """
        @desc: 从组织移除人员
        @params: user_unit_id add_staff返回的id/或者可以通过O2OAUnit().list获取 字段woSubDirectIdentityList.id
        """
        url = config.O2OA_URL + "/x_organization_assemble_control/jaxrs/identity/" + user_unit_id
        response = requests.delete(url, headers=self.o2oa_headers).json()
        types = response.get("type", "")
        if types != "success":
            raise O2OAError(response.get("message", ""))

        return response['data']


class O2OAUnitDuty(O2OAClient):

    def __init__(self):
        super(O2OAUnitDuty, self).__init__()

    def create(self, duty_name, unit_id):
        """
        @desc: 给组织添加职务
        @params: position_name 职务名称
        @params: 组织的id
        """
        url = config.O2OA_URL + "/x_organization_assemble_control/jaxrs/unitduty"

        data = {
            "attributeList": [""],
            "name": duty_name,
            "description": "",
            "unique": "",
            "unit": unit_id,
            "orderNumber": "",
            "identityList": [],
            "woIdentityList": []
        }

        response = requests.post(url, headers=self.o2oa_headers, data=json.dumps(data)).json()
        types = response.get("type", "")
        if types != "success":
            raise O2OAError(response.get("message", ""))

        return response['data']

    def delete(self, unit_duty_id):
        """
        @desc: 从组织移除职务
        @params: unit_position_id add_position返回的id /或者可以通过O2OAUnit().list获取 字段woUnitDutyList.id
        """
        url = config.O2OA_URL + "/x_organization_assemble_control/jaxrs/unitduty/" + unit_duty_id
        response = requests.delete(url, headers=self.o2oa_headers).json()
        types = response.get("type", "")
        if types != "success":
            raise O2OAError(response.get("message", ""))

        return response['data']


class O2OAUnitAttribute(O2OAClient):

    def __init__(self):
        super(O2OAUnitAttribute, self).__init__()

    def create(self, attribute_name, unit_id):
        """
        @desc: 给组织添加属性
        @params: position_name 职务名称
        @params: 组织的id
        """
        url = config.O2OA_URL + "/x_organization_assemble_control/jaxrs/unitattribute"
        data = {
            "attributeList": [""],
            "name": attribute_name,
            "description": "",
            "unique": "",
            "unit": unit_id,
            "orderNumber": "",
        }

        response = requests.post(url, headers=self.o2oa_headers, data=json.dumps(data)).json()
        types = response.get("type", "")
        if types != "success":
            raise O2OAError(response.get("message", ""))
        return response['data']

    def delete(self, unit_attribute_id):
        """
        @desc: 从组织移除属性
        @params: unit_attribute_id add_attribute返回的id
        """
        url = config.O2OA_URL + "/x_organization_assemble_control/jaxrs/unitattribute/" + unit_attribute_id
        response = requests.delete(url, headers=self.o2oa_headers).json()
        types = response.get("type", "")
        if types != "success":
            raise O2OAError(response.get("message", ""))

        return response['data']


if __name__ == '__main__':
    # 创建组织
    # print(O2OAUnit().create("第一个组织", "SDF023"))
    "3be3e183-8eeb-4035-bd02-c8a65e7de93d"

    # 有父级组织的情况
    # print(O2OAUnit().create("第一个组织2", "SDF0231", superior="3be3e183-8eeb-4035-bd02-c8a65e7de93d"))
    "4d1aa149-7600-4b23-b473-0c6ef322e084"

    # 删除组织
    # print(O2OAUnit().delete("9d2d8748-f2c3-484f-a61e-e26b2f7b4d98"))  #98ac3b5f-0996-48a4-8984-0c837898a9cd

    # 获取组织信息
    from rich import print_json

    print_json(json.dumps(O2OAUnit().list("4d1aa149-7600-4b23-b473-0c6ef322e084")))

    # 组织人员
    # 创建组织人员
    # print(O2OAUnitStaff().create("孟颖", "a21cbff4-51fb-42fc-8510-cac7e8ccf467", "4d1aa149-7600-4b23-b473-0c6ef322e084"))
    # ce6f7fab-5a2d-4b18-9448-40426d37176f

    # 删除组织人员
    # print(O2OAUnitStaff().delete('a21cbff4-51fb-42fc-8510-cac7e8ccf467'))

    # 组织职务
    # print(O2OAUnitDuty().create("小弟", "4d1aa149-7600-4b23-b473-0c6ef322e084"))
    # print(O2OAUnitDuty().delete("ebfa63b8-3f74-4909-a95f-e033b9bce52e"))

    # 组织属性
    # print(O2OAUnitAttribute().create("小弟", "4d1aa149-7600-4b23-b473-0c6ef322e084"))
    # print(O2OAUnitAttribute().delete("a4386382-3241-4884-b134-c99b262be399"))
